package com.ksd.pug.controller.category;

import com.ksd.pug.controller.BaseController;
import com.ksd.pug.pojo.ProductCategory;
import com.ksd.pug.service.category.IProductCategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: hcl
 * @date: 2022/1/24 14:59
 * @Description:
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class ProductCategoryController extends BaseController {

    private final IProductCategoryService CategoryService;
    /**
     * 查询产品分类
     *
     * @return
     */
    @GetMapping("/product/categories")
    public List<ProductCategory> findProductCategories() {
        return CategoryService.findCategories();
    }

    /**
     * 保存和修改分类
     *
     * @return
     */
    @PostMapping("/product/savecategory")
    public ProductCategory saveCategory(@RequestBody ProductCategory Category) {
        boolean flag = CategoryService.saveOrUpdate(Category);
        return flag ? Category : null;
    }

    /**
     * 根据id删除分类
     *
     * @return
     */
    @PostMapping("/product/category/del/{id}")
    public boolean saveCategory(@PathVariable("id") Long id) {
        ProductCategory category = new ProductCategory();
        category.setId(id);
        category.setIsdelete(1);
        return CategoryService.updateById(category);
    }

    /**
     * 根据id查询分类明细
     *
     * @return
     */
    @GetMapping("/product/category/get/{id}")
    public ProductCategory getCategory(@PathVariable("id") Long id) {
        return CategoryService.getById(id);
    }
}
